package 补充;
import java.io.*;
import java.net.*;
class Receiver implements Runnable {
    public void run() {
        try {
            DatagramSocket ds=new DatagramSocket(9090);
            byte[] buf=new byte[1024];
            DatagramPacket dp=new DatagramPacket(buf, buf.length);
            String line=null;
            do {
                ds.receive(dp);
                line=new String(dp.getData(),0,dp.getLength());
                System.out.println(line);
            }while(!line.equals("exit"));
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
class Sender implements Runnable {
    public void run() {
        try {
            DatagramSocket ds=new DatagramSocket();
            byte[] buf=new byte[1024];
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String line=null;
            DatagramPacket dp=null;
            do {
                line=br.readLine();
                buf=line.getBytes();
                dp=new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"),9090);
                ds.send(dp);

            }while(!line.equals("exit"));
        }  catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
public class 聊天程序 {
    public static void main(String[] args) {
        new Thread(new Sender()).start();
        new Thread(new Receiver()).start();
    }
}
